/**
* Class to manage static support for application
*
* Project: idbcrown
* Author:  TM
* Date:    22/12/2012
*
* History
* ------------------
* 22/12/2012 - TM - Initial version.
*/
 
package Application;

import java.io.*;
import java.util.*;

import Util.*;

public class libApplication
{
   //Configuration DB keys
   public final static String CONFIG_FILE = "./webapps/idbcrownINServer/WEB-INF/conf/INServer.txt";
   //Variables for class
   protected libIniFile configFile;   
   /** Singleton support */
   protected static libApplication staticApp;   
   

   /** Constructor for singleton without iniFile */
   protected libApplication() throws IOException
   {
     configFile = new libIniFile();
     configFile.load(CONFIG_FILE);
   }


   /** Singleton */
   public static libApplication getInstance()
   {
     try
     {
       if (staticApp == null)
         staticApp = new libApplication();
     }
     catch (Exception ex)
     {
       staticApp.showErrorMsg(ex);
     }
     return staticApp;
   }
   
   // **********************************
   // Application characterization
   // **********************************

   /** Get standard name for this application */
   public String getId()
   {
     return "idbcrownINServer";
   }
   
   // **********************************
   // Translation item support
   // **********************************
 
   /** Get translation item identified by key - if no configuration or not defined returns null */
   public String translate(String key)
   {
     return configFile != null ? (configFile.getProperty(key) != null ? configFile.getProperty(key) : key) : key;
   }
   
   // **********************************
   // Configuration item support
   // **********************************
 
   /** Get configuration item identified by key - if no configuration or not defined returns null */
   public String getConfigValue(String key)
   {
     return configFile != null ? configFile.getProperty(key) : null;
   }
 
   /** Get non-empty configuration item identified by key -
       if no configuration, not defined or empty returns null - result is trimmed */
   public String getConfigNonEmptyValue(String key)
   {
     String result = getConfigValue(key);
     if (result != null)
     {
       result = result.trim();
       if (result.equals(""))
         result = null;
     }
     return result;
   }
 
   /** Get configuration item identified by key as Integer -
       if no configuration, not defined or not an integer returns null
   */
   public Integer getConfigInteger(String key)
   {
     String value = getConfigNonEmptyValue(key);
     try
     {
       return value != null ? new Integer(value) : null;
     }
     catch (Exception except)
     {
       return null;
     }
   }
 
   /** Get configuration item identified by key as int
       if no configuration, not defined or not an integer, defaultValue is returned
   */
   public int getConfigInt(String key, int defaultValue)
   {
     Integer value = getConfigInteger(key);
     return value != null ? value.intValue() : defaultValue;
   }

   /** Get configuration item identified by key as Integer -
       if no configuration, not defined or not an integer returns null
   */
   public Double getConfigDouble(String key)
   {
     String value = getConfigNonEmptyValue(key);
     try
     {
       return value != null ? new Double(value) : null;
     }
     catch (Exception except)
     {
       return null;
     }
   }

   // **********************************
   //  SERVLET MESSAGE SUPPORT
   // **********************************
   
   /** Get the separator for values between fields (e.g. multiple values in a checkbox) */
   public String getSeparatorInsideField() {return getConfigValue("SEPARATOR_INSIDE_FIELD");}
   /** Get the separator for columns (e.g. multiple columns in a select) */
   public String getSeparatorForColumns () {return getConfigValue("SEPARATOR_BETWEEN_COLS");}
   /** Get the separator for rows (e.g. multiple rows in a select) */
   public String getSeparatorForRows    () {return getConfigValue("SEPARATOR_BETWEEN_ROWS");}

   
   // **********************************
   //  DIALOG SUPPORT
   // **********************************
   
   /** Shows a message box in an error way */
   public void showErrorMsg(Exception ex)
   {
     libFileDebug.getInstance().write(ex);
   }

   /** Shows a message box in an error way */
   public void showErrorMsg(String text)
   {
     showMessage("Error", text);
   }

   /** Shows a message box in an error way */
   public void showErrorMsg(String title, String text)
   {
     showMessage(title, text);
   }
   
   /** Shows a message box in an error way */
   public void showInfoMsg(String title, String text)
   {
     showMessage(title, text);
   }

   public void showMessage(String title, String text)
   {
     debug("------ "+ title + " ------");
     debug("------ "+ text);
   }
   
   /** Debug utility method */
   public void debug(String msg)
   {
     libFileDebug.getInstance().write("libApplication." + msg);
   }
   
}
